package com.example.hotelapp.Activity;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import com.danikula.videocache.HttpProxyCacheServer;
import com.example.hotelapp.Base.BaseActivity;
import com.example.hotelapp.Bean.UserRegisterBean;
import com.example.hotelapp.R;
import com.example.hotelapp.Utils.AppNetConfig;
import com.example.hotelapp.Utils.CustomVideoView;
import com.example.hotelapp.Utils.MyApp;
import com.example.utils.CommonUtils;
import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;
import com.kongzue.dialog.v2.WaitDialog;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

import static com.example.hotelapp.Utils.MyCommonUtils.ToastShow;
import static com.example.utils.CommonUtils.*;

/**
 * 登录或者注册页
 */
public class GuideActivity extends BaseActivity{
    @BindView(R.id.videoView)
    CustomVideoView videoView;
    @BindView(R.id.zhezhao)
    ImageView zhezhao;
    private Activity activity;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_guide;
    }

    /**
     * 初始化
     */
    @Override
    protected void init() {
        activity = this;
        Immerse(this);
        KeepScreenOn(this);
        setSystemBarColor(activity,R.color.transparent);
        HttpProxyCacheServer proxy = MyApp.getProxy(activity);
        String proxyUrl = proxy.getProxyUrl(AppNetConfig.VideoUrl);
        videoView.setVideoURI(Uri.parse(proxyUrl));
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {

            }
        });
        //循环播放
        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                videoView.start();
            }
        });
        videoView.start();//开始播放
    }

    /**
     * 点击事件
     */
    @OnClick({R.id.register, R.id.youke})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.register:
               SendCodeRegister(activity);
                break;
            case R.id.youke:
                IntentToPage(activity,LoginActivity.class);
                break;
        }
    }

    /**
     * 注册
     */
    public void SendCodeRegister(Context context) {
        RegisterPage page = new RegisterPage();
        //如果使用我们的ui，没有申请模板编号的情况下需传null
        page.setTempCode(null);
        page.setRegisterCallback(new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_COMPLETE) {
                    // 处理成功的结果
                    HashMap<String,Object> phoneMap = (HashMap<String, Object>) data;
                    // 国家代码，如“86”
                    String country = (String) phoneMap.get("country");
                    // 手机号码，如“13800138000”
                    String phone = (String) phoneMap.get("phone");
                    //成功回调
                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                        RegisterAction(phone);
                    }
                }
            }
        });
        page.show(context);
    }

    /**
     * 注册
     */
    private void RegisterAction(String phone) {
        WaitDialog.show(activity,"注册中...");
        OKPostValue("phone", phone,"photo","profile.jpg" ,AppNetConfig.Register, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                WaitDialog.dismiss();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ToastShow(activity, "服务器出错，请稍后再试");
                    }
                });
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                WaitDialog.dismiss();
                //获取返回的json数据并解析
                String json = response.body().string();
                System.out.println(json);
                UserRegisterBean databean = new Gson().fromJson(json, UserRegisterBean.class);
                int code = databean.getCode();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (code == 1) {
                            SaveInfo(activity,"GetPhone","phone",phone);
                            SaveInfo(activity,"GetUid","id", String.valueOf(databean.getData().getId()));
                            IntentToPage(activity,MainActivity.class);
                        } else {
                            ToastShow(activity,"注册失败");
                        }
                    }
                });
            }
        });
    }

    /**
     * 生命周期onResume
     */
    @Override
    protected void onResume() {
        super.onResume();
        videoView.start();
        videoView.isPlaying();
    }

    /**
     * 生命周期onRestart
     */
    @Override
    protected void onRestart() {
        super.onRestart();
        videoView.start();
        videoView.isPlaying();
    }

    /**
     * 生命周期onPause
     */
    @Override
    protected void onPause() {
        super.onPause();
        videoView.pause();
    }
}
